#!/bin/sh

# File        :  postinstall
#
# Purpose     :  execute all tasks necessary following installation of 
#                Privoxy's files
#
# Copyright   :  Written by and Copyright (C) 2001-2012 the
#                Privoxy team. http://www.privoxy.org/
#
#                This program is free software; you can redistribute it
#                and/or modify it under the terms of the GNU General
#                Public License as published by the Free Software
#                Foundation; either version 2 of the License, or (at
#                your option) any later version.
#
#                This program is distributed in the hope that it will
#                be useful, but WITHOUT ANY WARRANTY; without even the
#                implied warranty of MERCHANTABILITY or FITNESS FOR A
#                PARTICULAR PURPOSE.  See the GNU General Public
#                License for more details.
#
#                The GNU General Public License should be included with
#                this file.  If not, you can view it at
#                http://www.gnu.org/copyleft/gpl.html
#                or write to the Free Software Foundation, Inc.,
#                51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
#                USA
#
# Modification : If you modify this file please consider whether your 
#                changes ought to be passed back to the OSXPackageBuilder
#                module.
#

#  This postinstall script:
#
#  1. Moves config files into place, respecting any existing user config
#  2. Creates links to documentation in the app folder
#  3. Detects the version of OS X on which we're installing
#  4. Creates the logfile if not found and sets its ownership and persmissions
#  5. Disables the startup method not necessary for the host's OS X version and start Privoxy
#  6. Opens the readme.rtf file for the user to read

# preinstall created this file; continue to append to it in this script
logfile='/var/privoxy_installation.log'

#  1. Move config files into place, setting ownership and permissions, respecting any existing user config
#
for i in default.action default.filter match-all.action config trust user.action user.filter templates; do
	if [ "$i" = "default.action" ] || [ "$i" = "default.filter" ] ; then
		# for the files that the end user should not edit, overwrite existing, older versions
		echo 'Installing config file (overwriting existing file if present):' $i >> ${logfile}
		/bin/rm -f /usr/local/etc/privoxy/$i
		/usr/bin/install -c -m 0664 -o privoxy -g privoxy /usr/local/etc/privoxy/vanilla/$i /usr/local/etc/privoxy || exit 1;
	elif [ "$i" = "templates" ]; then
		# for the templates subfolder copy across all the templates as .new if they already exist
		for j in `ls -A /usr/local/etc/privoxy/vanilla/templates`; do
			if [ -s "/usr/local/etc/privoxy/templates/"$j ]; then
				# if the template exists in the destination then copy it in as <filename>.new
				echo 'Installing template file with .new extension:' $j >> ${logfile}
				/usr/bin/install -c -m 0664 -o privoxy -g privoxy /usr/local/etc/privoxy/vanilla/templates/$j /usr/local/etc/privoxy/templates/$j.new || exit 1
			else
				# if the template doesn't exist in the destination then just copy it across
				echo 'Installing template file:' $j >> ${logfile}
				/usr/bin/install -c -m 0664 -o privoxy -g privoxy /usr/local/etc/privoxy/vanilla/templates/$j /usr/local/etc/privoxy/templates/$j || exit 1
			fi
		done
	elif [ -s "/usr/local/etc/privoxy/"$i ]; then
		# for all other files, if they already exist in the destination then copy them in as <filename>.new
		echo 'Installing config file with .new extension:' $i >> ${logfile}
		/usr/bin/install -c -m 0664 -o privoxy -g privoxy /usr/local/etc/privoxy/vanilla/$i /usr/local/etc/privoxy/$i.new || exit 1
	else
		# for all files that don't already exist just copy them across
		echo 'Installing config file:' $i >> ${logfile}
		/usr/bin/install -c -m 0664 -o privoxy -g privoxy /usr/local/etc/privoxy/vanilla/$i /usr/local/etc/privoxy || exit 1
	fi
done
# delete the vanilla config files
/bin/rm -rf /usr/local/etc/privoxy/vanilla >> ${logfile} 2>&1

# 2. Create links to documentation and log file in the app folder
#
ln /usr/local/share/doc/privoxy/AUTHORS /Applications/Privoxy/
ln /usr/local/share/doc/privoxy/ChangeLog /Applications/Privoxy/
ln -s /usr/local/share/doc/privoxy/privoxy-index.html /Applications/Privoxy/Privoxy\ Documentation.html
ln /usr/local/share/doc/privoxy/LICENSE /Applications/Privoxy/
ln -s /var/log/privoxy/logfile.log /Applications/Privoxy/logfile.log

# 3. Detect the version of OS X on which we're installing
#
darwin_major_rel_num="`/usr/bin/uname -r | /usr/bin/sed 's/\..*//'`"

# 4. Create logfile if not found and set its ownership and persmissions
#
if [ ! -d /var/log/privoxy ]; then
	echo 'Creating Privoxy logfile directory' >> ${logfile}
	/bin/mkdir -m 0755 /var/log/privoxy >> ${logfile} 2>&1
fi
echo 'Creating Privoxy logfile and setting owner and permissions' >> ${logfile}
/usr/bin/touch /var/log/privoxy/logfile.log >> ${logfile} 2>&1
/usr/sbin/chown privoxy:privoxy /var/log/privoxy/logfile.log >> ${logfile} 2>&1
/bin/chmod 0644 /var/log/privoxy/logfile.log >> ${logfile} 2>&1

# 5. Disable the startup method not necessary for the host's OS X version and start Privoxy
#
case "${darwin_major_rel_num}" in
  # Mac OS X 10.5 or higher
  9|1*)
		# delete Privoxy StartupItem
		echo 'Delete the Privoxy StartupItem since it is not required for this OS X version' >> ${logfile}
		if [ -d /Library/StartupItems/Disabled/Privoxy ]; then
			/bin/rm -rf /Library/StartupItems/Disabled/Privoxy >> ${logfile} 2>&1
		fi
		/bin/rm -rf /Library/StartupItems/Privoxy >> ${logfile} 2>&1
		# start Privoxy using launchd
		echo 'Start Privoxy via the LaunchDaemon' >> ${logfile}
		/bin/launchctl load /Library/LaunchDaemons/org.ijbswa.privoxy.plist >> ${logfile} 2>&1
  ;;
  # Mac OS X 10.4, 10.3
  8|7)
		# can safely delete this since these older OS X releases do not support launchd anyway
		echo 'Delete the Privoxy LaunchDaemon since it is not required for this OS X version' >> ${logfile}
		/bin/rm -f /Library/LaunchDaemons/org.ijbswa.privoxy.plist
		# start Privoxy using StartupItem
		echo 'Start Privoxy via the StartupItem' >> ${logfile}
		/Library/StartupItems/Privoxy/Privoxy start >> ${logfile} 2>&1
  ;;
  # default
  *)
    ;;
esac

# 6. Open the readme.rtf file for the user to read
/usr/bin/su $USER -c "/usr/bin/open /Applications/Privoxy/readme.rtfd"

/bin/mv ${logfile} /Applications/Privoxy/install.log

exit 0
